package com.example.upload.controller.file;

import com.example.upload.service.file.FileService;
import com.example.upload.response.ResponseResult;
import com.example.upload.response.ResponseResultFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;

/**
 * (File)表控制层
 *
 * @author makejava
 * @since 2024-05-28 11:28:01
 */
@RestController
@RequestMapping("/fileStorage/file/")
public class FileController {
    /**
     * 服务对象
     */
    @Autowired
    private FileService fileService;

    @GetMapping("/getMyFile")
    public ResponseResult getMyFile(@RequestParam(value = "keyWords") String keyWord,
                                    @RequestParam(value = "page",defaultValue = "1") Integer page,
                                    @RequestParam(value = "PageSize",defaultValue = "10") Integer pageSize) {
        return ResponseResultFactory.buildSuccessResponse(this.fileService.getMyFile(page,pageSize,keyWord));
    }

    /**
     * 上传文件
     *
     * @param file 实体
     * @return 新增结果
     */
    @PostMapping("/upload")
//    @LoginStatus
    public ResponseResult add(MultipartFile file) {
        return ResponseResultFactory.buildSuccessResponse(this.fileService.insert(file));
    }


    /**
     * 下载文件
     * @param filePath
     * @param response
     * @return
     */
    @GetMapping("/download")
//    @LoginStatus
    public ResponseResult add(String filePath, HttpServletResponse response) {
        fileService.downLoadFile(filePath, response);
        return ResponseResultFactory.buildSuccessResponse();
    }

}

